<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 角色列表卡片视图 -->
    <el-card>
      <el-button type="primary">添加角色</el-button>
      <el-table :data = "rolesList" border stripe>
        <!-- 权限树状图 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row  v-for="(item, i1) in scope.row.children" :key = "item.id" :class="['vcenter', 'border_bottom', i1 == '0'? 'bdtop':'']" >
              <!-- 第一级 -->
              <el-col :span="5">
                <el-tag closable :disable-transitions="true" @close="deleteTheRight(scope.row, item.id)">{{item.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 第二级 -->
              <el-col :span="19">
                <el-row v-for="(it, i2) in item.children" :key = "it.id" :class="['vcenter', i2 == '0'? '':'bdtop' ]">
                  <el-col :span="6">
                    <el-tag type="success" closable :disable-transitions="true" @close="deleteTheRight(scope.row, it.id)">{{it.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 第三级 -->
                  <el-col :span="18" >
                    <el-tag closable :disable-transitions="true" @close="deleteTheRight(scope.row, it3.id)" v-for="it3 in it.children" :key = "it3.id" type="warning">{{it3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- <pre>{{scope.row}}</pre> -->
          </template>
        </el-table-column>
        <el-table-column type="index" label="#" width="80"> </el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            <el-button type="warning" icon="el-icon-s-tools" size="mini" @click="showRightDialog(scope.row)" >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限对话框 -->
    <el-dialog title="分配权限" :visible.sync="setRightDialogVisible" width="50%" @close="closeRightDialog">
      <el-tree :data="rights_list" show-checkbox node-key="id" default-expand-all
      :default-checked-keys="defkeys" :props="defaultProps" ref="tree">
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button @click="setRightDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRights()" >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  created () {
    this.getRoleList()
  },
  data () {
    return {
      rolesList: [],
      setRightDialogVisible: false,
      rights_list: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      defkeys: [],
      roleId: ''
    }
  },
  methods: {
    async getRoleList () {
      const { data: res } = await this.$http.get('/roles')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.rolesList = res.data
      console.log(res)
    },
    edit (res) {
    },
    // 删除指定权限
    async deleteTheRight (role, rightId) {
      const data = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(error => error)
      if (data === 'cancel') {
        return this.$message({ type: 'info', message: '已取消删除' })
      }
      const { data: res } = await this.$http.delete(`/roles/${role.id}/rights/${rightId}`)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      role.children = res.data
      this.$message.success(res.meta.msg)
    },
    // 展示权限列表
    async showRightDialog (roles) {
      this.roleId = roles.id
      // 获取所有权限
      const { data: res } = await this.$http.get('rights/tree')
      // console.log(res.data)
      this.rights_list = res.data
      // 获取已授权权限
      this.getrights3(roles, this.defkeys)
      this.setRightDialogVisible = true
    },
    // 递归获取三级权限
    getrights3: function (node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(element => {
        this.getrights3(element, arr)
      })
    },
    // 角色授权
    async setRights () {
      // 扩展符拼接
      const keys = [...this.$refs.tree.getCheckedKeys(), ...this.$refs.tree.getHalfCheckedKeys()]
      const idstr = keys.join(',')
      const { data: res } = await this.$http.post(`roles/${this.roleId}/rights`, { rids: idstr })
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.getRoleList()
      this.setRightDialogVisible = false
    },
    // 关闭权限对话框
    closeRightDialog: function () {
      this.defkeys = []
      this.setRightDialogVisible = false
    }
  }
}
</script>
<style lang="less" scoped>
.el-table{
  margin-top: 20px;
}
.el-tag{
  margin: 7px;
}
.bdtop{
  border-top: 1px solid gray;
}
.border_bottom{
  border-bottom: 1px solid gray;
}
.vcenter{
  display:flex;
  align-items: center;
}
</style>
